/*
 * 转码模板的创建和查询
 */

var pageIndex = 0;
var pageSize = 10;
var pageCount = 0;
var searchInput = {
	name: "",
	favorite: "",
	type: ""
}

document.querySelector("#create-format").addEventListener("change", function() {
	if(document.querySelector("#create-format").value == "HLS") {
		document.querySelector("#encryption-div").style.display = "";
	} else {
		document.querySelector("#encryption-div").style.display = "none";
	}
	
	if(document.querySelector("#create-format").value == "FLV") {
		var dom = document.querySelector("#create-codecVideo");
		dom.value = "H264";
		dom[1].disabled = true;
	} else {
		var dom = document.querySelector("#create-codecVideo");
		document.querySelector("#create-codecVideo")[1].disabled = false;
	}
});

function createTemplate() {
	var templateModal = document.getElementsByClassName("template-modal")[0];
	templateModal.style.display = "";
}

function cancel() {
	document.querySelector("#create-name").value = "";
	document.querySelector("#create-favorite").value = "";
	document.querySelector("#create-format").value = "MP4";
	document.querySelector("#create-encrytion").value = "true";
	document.querySelector("#create-codecVideo").value = "H264";
	document.querySelector("#create-bitRateVideo").value = "";
	document.querySelector("#create-frameRateVideo").value = "";
	document.querySelector("#create-heightVideo").value = "";
	document.querySelector("#create-widthVideo").value = "";
	document.querySelector("#create-autoRotateVideo").value = "true";
	document.querySelector("#create-codecAudio").value = "AAC";
	document.querySelector("#create-bitRateAudio").value = "";
	document.querySelector("#create-sampleRateAudio").value = "44100";
	
	var templateModal = document.getElementsByClassName("template-modal")[0];
	templateModal.style.display = "none";
}

function confirm() {
	var name = document.querySelector("#create-name").value;
	var favorite = document.querySelector("#create-favorite").value;
	var format = document.querySelector("#create-format").value;
	var encryption = document.querySelector("#create-encrytion").value;
	var video = {};
	video.codec = document.querySelector("#create-codecVideo").value;
	video.bitRate = document.querySelector("#create-bitRateVideo").value + "k";
	video.frameRate = document.querySelector("#create-frameRateVideo").value;
	video.height = document.querySelector("#create-heightVideo").value;
	video.width = document.querySelector("#create-widthVideo").value;
	video.autoRotate = document.querySelector("#create-autoRotateVideo").value;
	var audio = {};
	audio.codec = document.querySelector("#create-codecAudio").value;
	audio.bitRate = document.querySelector("#create-bitRateAudio").value + "k";
	audio.sampleRate = document.querySelector("#create-sampleRateAudio").value;
	
	var params = {
		name: name,
		favorite: favorite,
		format: format,
		encryption: encryption,
		video: video,
		audio: audio
	}
	
	ajax({
		url: "/template/transcode/create",
		type: "POST",
		contentType: "application/json",
		data: JSON.stringify(params),
		dataType: "json",
		success: function(response) {
			var res = JSON.parse(response);
			if(res.code == "0") {
				alert("转码模板创建成功");
				cancel();
			} else {
				alert(res.message);
			}
		},
		error: function(status) {
			
		}
	});
}

function search() {
	var name = document.querySelector("#templateName").value;
	var favorite = document.querySelector("#favoriteFlag").value;
	var type = document.querySelector("#templateType").value;
	searchInput.name = name;
	searchInput.favorite = favorite;
	searchInput.type = type;
	var params = {
		name: searchInput.name,
		favorite: searchInput.favorite,
		type: searchInput.type,
		pageSize: pageSize,
		pageIndex: pageIndex
	}
	getTemplateList(params);
}

function prePage() {
	if(pageIndex == 0) {
		return;
	}
	pageIndex = pageIndex - 1;
	var params = {
   		name: searchInput.name,
   		favorite: searchInput.favorite,
   		type: searchInput.type,
   		pageSize: pageSize,
   		pageIndex: pageIndex
   	}
   	getTemplateList(params);
}

function nextPage() {
	if(pageIndex + 1 >= pageCount) {
		return;
	}
	pageIndex = pageIndex + 1;
	var params = {
   		name: searchInput.name,
   		favorite: searchInput.favorite,
   		type: searchInput.type,
   		pageSize: pageSize,
   		pageIndex: pageIndex
   	}
   	getTemplateList(params);
}

function getTemplateList(params) {
	ajax({
		url: "/template/transcode/search",
		type: "post",
		contentType: "application/json",
		data: JSON.stringify(params),
		dataType: "json",
		success: function(responseText) {
			var res = JSON.parse(responseText);
			if(res.code == "0") {
				var html = "";
				var list = res.data;
				pageCount = res.pageCount;
				for(var i = 0; i < list.length; i++) {
					var obj = list[i];
					html += "<tr><td>" + obj.templateId + "</td><td>" + obj.name + "</td><td>"
							+ obj.type + "</td><td>" + obj.favorite + "</td><td>"
							+ obj.format + "</td><td>" + obj.encrytion + "</td><td>"
							+ obj.video.codec + "</td><td>" + obj.video.bitRate + "</td><td>"
							+ obj.video.frameRate + "</td><td>" + obj.video.height + "</td><td>"
							+ obj.video.width + "</td><td>" + obj.video.autoRotate + "</td><td>"
							+ obj.audio.codec + "</td><td>" + obj.audio.bitRate + "</td><td>"
							+ obj.audio.sampleRate + "</td></tr>";
				}
				var tbody = document.querySelector("#template-list");
				tbody.innerHTML = html;
				document.querySelector("#page-index").innerText = "第" + (pageIndex+1) + "/" + pageCount + "页";
			}
		},
		error: function(status) {
			
		}
	});
}